package com.daguchuangyi.erp_android.productlist.productsummary.widge;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jude.utils.JUtils;

/**
 * Created by lijianjian on 18/3/19.
 */

public class SummaryDecoration extends RecyclerView.ItemDecoration {
    private static final String TAG = "SpaceDecoration";

    private int mLeftRight;
    private int mTopBottom;
    private int mGap;

    public SummaryDecoration(int leftRight, int gap, int topBottom) {
        this.mLeftRight = leftRight;
        this.mTopBottom = topBottom;
        mGap = gap;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        JUtils.Log(TAG, "getItemOffsets was called: ");
//        if (parent.getLayoutManager() instanceof LinearLayoutManager) {
//            return;
//        }
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();

        BaseQuickAdapter quickAdapter = (BaseQuickAdapter) parent.getAdapter();
        int mHeaderCount = quickAdapter.getHeaderLayoutCount();
        int footerCount = quickAdapter.getFooterLayoutCount();

        //判断总的数量是否可以整除
        int totalCount = layoutManager.getItemCount() - mHeaderCount - footerCount;
        int surplusCount = totalCount % layoutManager.getSpanCount();
        int childPosition = parent.getChildAdapterPosition(view);
        JUtils.Log(TAG, "child position is : " + childPosition);

        if (childPosition < mHeaderCount) {
            outRect.set(0, 0, 0, 0);
            return;
        }

        childPosition -= mHeaderCount;
        if (layoutManager.getOrientation() == GridLayoutManager.VERTICAL) {//竖直方向的
            if (surplusCount == 0 && childPosition > totalCount - layoutManager.getSpanCount() - 1) {
                //后面几项需要bottom
                outRect.bottom = mTopBottom;
            } else if (surplusCount != 0 && childPosition > totalCount - surplusCount - 1) {
                outRect.bottom = mTopBottom;
            }
            if ((childPosition + 1) % layoutManager.getSpanCount() == 0) {//被整除的需要右边
                outRect.right = mLeftRight;
                outRect.left = mGap / 2;
            } else if ((childPosition + 1) % layoutManager.getSpanCount() == 1) {
                outRect.left = mLeftRight;
                outRect.right = mGap / 2;
            }
            outRect.top = mTopBottom;
//            outRect.left = mLeftRight;
        } else {
            if (surplusCount == 0 && childPosition > totalCount - layoutManager.getSpanCount() - 1) {
                //后面几项需要右边
                outRect.right = mLeftRight;
            } else if (surplusCount != 0 && childPosition > totalCount - surplusCount - 1) {
                outRect.right = mLeftRight;
            }
            if ((childPosition + 1) % layoutManager.getSpanCount() == 0) {//被整除的需要下边
                outRect.bottom = mTopBottom;
            }
            outRect.top = mTopBottom;
            outRect.left = mLeftRight;
        }
    }

}
